package org.jdsm.flow.core.users.impl.jpa.dao;


import org.jdsm.flow.core.users.IUser;
import org.jdsm.flow.core.users.impl.jpa.User;
import org.jdsm.flow.core.utils.impl.jpa.GenericDao;
import org.jdsm.flow.core.utils.impl.jpa.PersistenceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import java.util.Collection;
import java.util.LinkedHashSet;

/**
 * User: Michael Komarichin aka mikom
 * Date: 13.06.2010
 * Time: 13:54:30
 */


public class UserDAO extends GenericDao<User,Long> {

    private static final Logger log = LoggerFactory.getLogger(UserDAO.class);

 /*   @Override
    public IUser findUserById(Long identifier) {
        if (identifier == null) {
            log.warn("user's identifier can't be null");
            return null;
        }
        return getEntityManager().find(User.class, identifier);
    }

    @Override
    public IRole findRoleById(Long id) {
        if (id == null) {
            log.warn("role's identifier can't be null");
            return null;
        }
        return getEntityManager().find(Role.class, id);
    }

    @Override
    public IGroup findGroupById(Long id) {
        if (id == null) {
            log.warn("group's identifier can't be null");
            return null;
        }
        return getEntityManager().find(Group.class, id);
    }
*/

    public IUser load(IUser user) {
        IUser result = null;
        if (user == null) {
            log.warn("user can't be null");
            return null;
        }
        if (user.getId() != null) {
            result = find((Long) user.getId());
        }
        if (result == null && user.getName() != null) {
            result = findByName(user.getName());
        }
        log.debug("found user: {}", result);
        return result;
    }

    /*@Override
    public IRole loadRole(IRole role) {
        IRole result = null;
        if (role == null) {
            log.warn("role can't be null");
            return null;
        }
        if (role.getId() != null) {
            result = findRoleById((Long) role.getId());
        }
        if (result == null && role.getName() != null) {
            result = findRoleByName(role.getName());
        }
        log.debug("found role: {}", result);
        return result;
    }

    @Override
    public IGroup loadGroup(IGroup group) {
        IGroup result = null;
        if (group == null) {
            log.warn("role can't be null");
            return null;
        }
        if (group.getId() != null) {
            result = findGroupById((Long) group.getId());
        }
        if (result == null && group.getName() != null) {
            result = findGroupByName(group.getName());
        }
        log.debug("found group: {}", result);
        return result;
    }*/

/*    @Override
    public Collection<IRole> findUserRolesByUserId(Long identifier) {
        if (identifier == null) {
            log.warn("user's identifier can't be null");
            return EMPTY_ROLES_SET;
        }
        try {
            TypedQuery<Role> tq = getEntityManager().createNamedQuery("user.findRoles", Role.class)
                    .setParameter("userId", identifier);
            List<Role> rs = tq.getResultList();
            if (log.isDebugEnabled()) {
                for (IRole role : rs) {
                    log.debug("role in result: {}", role);
                }
            }
            return new HashSet<IRole>(rs);
        } catch (NoResultException e) {
            log.warn("could not found roles by user id:{}", identifier);
            log.debug("trace: ", e);
            return EMPTY_ROLES_SET;
        }
    }*/


/*    @Override
    public Collection<IGroup> findUserGroupsByUserId(Long identifier) {
        if (identifier == null) {
            log.warn("user's identifier can't be null");
            return EMPTY_GROUPS_SET;
        }
        try {
            TypedQuery<Group> tq = getEntityManager().createNamedQuery("user.findGroups", Group.class)
                    .setParameter("userId", identifier);
            List<Group> gs = tq.getResultList();
            if (log.isDebugEnabled()) {
                for (IGroup group : gs) {
                    log.debug("group in result: {}", group);
                }
            }
            return new HashSet<IGroup>(gs);
        } catch (NoResultException e) {
            log.warn("could not found  groups by user id:{}", identifier);
            log.debug("trace: ", e);
            return EMPTY_GROUPS_SET;
        }
    }*/


/*    @Override
    public Collection<IUser> findUsersForGroup(IGroup group) {
        if (group == null) {
            log.warn("role can't be null");
            return EMPTY_USERS_SET;
        }
        Group g = (Group) loadGroup(group);
        if (g == null) {
            log.warn("can't found role : {}", group);
            return EMPTY_USERS_SET;
        }
        try {
            TypedQuery<User> tq = getEntityManager().createNamedQuery("group.findUsers", User.class)
                    .setParameter("group", g);
            List<User> us = tq.getResultList();
            if (log.isDebugEnabled()) {
                for (IUser user : us) {
                    log.debug("user in result: {}", user);
                }
            }
            return new HashSet<IUser>(us);
        } catch (NoResultException e) {
            log.warn("could not found users by group:{}", groupCount());
            log.debug("trace: ", e);
            return EMPTY_USERS_SET;
        }
    }*/

/*    @Override
    public Collection<IUser> findUsersForRole(IRole role) {
        if (role == null) {
            log.warn("role can't be null");
            return EMPTY_USERS_SET;
        }
        Role r = (Role) loadRole(role);
        if (r == null) {
            log.warn("can't found role : {}", role);
            return EMPTY_USERS_SET;
        }
        try {
            TypedQuery<User> tq = getEntityManager().createNamedQuery("role.findUsers", User.class)
                    .setParameter("role", r);
            List<User> us = tq.getResultList();
            if (log.isDebugEnabled()) {
                for (IUser user : us) {
                    log.debug("user in result: {}", user);
                }
            }
            return new HashSet<IUser>(us);
        } catch (NoResultException e) {
            log.warn("could not found users by role:{}", role);
            log.debug("trace: ", e);
            return EMPTY_USERS_SET;
        }
    }*/

//    @Override
    public Collection<IUser> load(int maxResult, int startResult, boolean isSort, String field) {
        return new LinkedHashSet<IUser>(PersistenceUtils.loadedByCriteria(getEntityManager(), User.class, maxResult, startResult, isSort, field));
    }

/*    @Override
    public Collection<IGroup> loadGroupsByCriteria(int maxResult, int startResult, boolean isSort, String field) {
        return new LinkedHashSet<IGroup>(PersistenceUtils.loadedByCriteria(getEntityManager(), Group.class, maxResult, startResult, isSort, field));
    }

    @Override
    public Collection<IRole> loadRolesByCriteria(int maxResult, int startResult, boolean isSort, String field) {
        return new LinkedHashSet<IRole>(PersistenceUtils.loadedByCriteria(getEntityManager(), Role.class, maxResult, startResult, isSort, field));
    }*/

/*    @Override
    public IUser removeUser(Long id) {
        IUser user = null;
        try {
            em.getTransaction().begin();
            user = findUserById(id);
            if (user != null) {
                em.remove(user);

            }
            em.getTransaction().commit();
            log.info("user {} was removed", user);
        } catch (Exception e) {
            log.error("could not remove user by id: {}", id);
            log.debug("trace:", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
        return user;
    }*/


/*
    @Override
    public IRole removeRole(Long id) {
        IRole role = null;
        try {
            em.getTransaction().begin();
            role = findRoleById(id);
            if (role != null) {
                em.remove(role);

            }
            em.getTransaction().commit();
            log.info("role {} was removed", role);
        } catch (Exception e) {
            log.error("could not remove role by id: {}", id);
            log.debug("trace:", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
        return role;
    }


    @Override
    public IGroup removeGroup(Long id) {
        IGroup group = null;
        try {
            em.getTransaction().begin();
            group = findGroupById(id);
            if (group != null) {
                em.remove(group);
            }
            em.getTransaction().commit();
            log.info("group {} was removed", group);
        } catch (Exception e) {
            log.error("could not remove group by id: {}", id);
            log.debug("trace:", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
        return group;
    }
*/


/*    @Override
    public IUser saveOrUpdate(IUser user) {
        User u;
        try {
            em.getTransaction().begin();
            u = (User) load(user);
            if (u == null) {
                em.persist(user);
                log.info("user: {} was saved", user);
            } else {
                em.merge(user);
                log.info("user {} was updated", user);
            }
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("could not save or update user: {} ", user);
            log.debug("trace: ", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
        return user;
    }

    @Override
    public IRole saveOrUpdate(IRole role) {
        IRole r;
        try {
            em.getTransaction().begin();
            r = loadRole(role);
            if (r == null) {
                em.persist(role);
                log.info("role {} was saved", role);
            } else {
                em.merge(role);
                log.info("role {} was updated", role);
            }
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("could not save or update role: {}", role);
            log.debug("trace:", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
        return role;
    }

    @Override
    public IGroup saveOrUpdate(IGroup group) {
        IGroup g;
        try {
            em.getTransaction().begin();
            g = loadGroup(group);
            if (g == null) {
                em.persist(group);
                log.info("group {} was saved", group);
            } else {
                em.merge(group);
                log.info("group {} was updated", group);
            }
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("could not save or update group: {}", group);
            log.debug("trace:", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
        return group;
    }*/

//    @Override
    public IUser findByName(String name) {
        log.debug("find user by name: {}", name);
        if (name == null) {
            log.warn("user name is null");
            return null;
        }

        try {

            return getEntityManager().createNamedQuery("user.findUserByName", User.class)
                    .setParameter("userName", name).getSingleResult();
        } catch (NoResultException e) {
            log.warn("could not found user by name:{}", name);
            log.debug("trace: ", e);
            return null;
        } catch (NonUniqueResultException e) {
            log.warn("non unique user by name: {}", name);
            log.debug("trace: ", e);
            return null;
        }
    }

//    @Override
    public IUser findByNickName(String nickName) {
        log.debug("find user by nick name: {}", nickName);
        if (nickName == null) {
            log.warn("user nick name is null");
            return null;
        }
        try {
            return getEntityManager().createNamedQuery("user.findUserByNickName", User.class)
                    .setParameter("nickName", nickName).getSingleResult();
        } catch (NoResultException e) {
            log.warn("could not found user by nickName:{}", nickName);
            log.debug("trace: ", e);
            return null;
        } catch (NonUniqueResultException e) {
            log.warn("non unique user by nickName: {}", nickName);
            log.debug("trace: ", e);
            return null;
        }
    }

/*    @Override
    public IRole findRoleByName(String name) {
        log.debug("find role by name: {}", name);
        if (name == null) {
            log.warn("role  name is null");
            return null;
        }
        try {
            return em.createNamedQuery("role.findRoleByName", Role.class)
                    .setParameter("roleName", name).getSingleResult();
        } catch (NoResultException e) {
            log.warn("could not found role by name:{}", name);
            log.debug("trace: ", e);
            return null;
        } catch (NonUniqueResultException e) {
            log.warn("non unique role by name: {}", name);
            log.debug("trace: ", e);
            return null;
        }
    }*/

/*    @Override
    public IGroup findGroupByName(String name) {
        log.debug("find group by name: {}", name);
        if (name == null) {
            log.warn("group name is null");
            return null;
        }
        try {
            return em.createNamedQuery("group.findGroupByName", Group.class)
                    .setParameter("groupName", name).getSingleResult();
        } catch (NoResultException e) {
            log.warn("could not found group by name:{}", name);
            log.debug("trace: ", e);
            return null;
        } catch (NonUniqueResultException e) {
            log.warn("non unique group by name: {}", name);
            log.debug("trace: ", e);
            return null;
        }
    }*/


//    @Override
    public int count() {
        return PersistenceUtils.count(getEntityManager(), User.class).intValue();
    }

 /*   @Override
    public int rolesCount() {
        return PersistenceUtils.count(em, Role.class).intValue();
    }

    @Override
    public int groupCount() {
        return PersistenceUtils.count(em, Group.class).intValue();
    }
*/

/*    @Override
    public void includeUserToRoles(IUser user, Collection<Long> roleIds) {
        log.info("include user: {} to role by ids: {}", user, roleIds);
        try {
            em.getTransaction().begin();
            User u = (User) load(user);
            if (u == null) {
                log.warn("can't found user:{}", user);
                return;
            }
            if (roleIds == null || roleIds.isEmpty()) {
                log.warn("roles can't be empty");
                return;
            }
            List<Role> roles = PersistenceUtils.loadEntitiesByIds(em, Role.class, roleIds);
            roles.addAll(u.getRoles());
            u.setRoles(new HashSet<Role>(roles));
            em.merge(u);
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("can't include user {} to roles {}", user, roleIds);
            log.debug("", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
    }*/

/*    @Override
    public void includeUserToGroups(IUser user, Collection<Long> groupIds) {
        log.info("include user {} to groups by ids:{}", user, groupIds);
        try {
            em.getTransaction().begin();
            User u = (User) load(user);
            if (u == null) {
                log.warn("can't found user:{}", user);
                return;
            }
            if (groupIds == null || groupIds.isEmpty()) {
                log.warn("groups  can't be empty");
                return;
            }
            List<Group> groups = PersistenceUtils.loadEntitiesByIds(em, Group.class, groupIds);
            groups.addAll(u.getGroups());
            u.setGroups(new HashSet<Group>(groups));
            em.merge(u);
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("can't include user {} to groups {}", user, groupIds);
            log.debug("", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }

    }*/


/*    @Override
    public void excludeUserFromRoles(IUser user, Collection<Long> roleIds) {
        log.info("exclude user {} from roles:{}", user, roleIds);
        try {
            em.getTransaction().begin();
            User u = (User) load(user);
            if (u == null) {
                log.warn("can't found user:{}", user);
                return;
            }
            if (roleIds == null || roleIds.isEmpty()) {
                log.warn("roles can't be empty");
                return;
            }
            Set<Long> newRoles = new HashSet<Long>();
            for (Role r : u.getRoles()) {
                newRoles.add(r.getId());
            }
            newRoles.removeAll(roleIds);

            Set<Role> uRoles = new HashSet<Role>();

            if (!newRoles.isEmpty()) {
                List<Role> roles = PersistenceUtils.loadEntitiesByIds(em, Role.class, newRoles);
                uRoles.addAll(roles);
            }
            u.setRoles(uRoles);
            em.merge(u);
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("can't exclude user {} from roles {}", user, roleIds);
            log.debug("", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
    }*/

/*    @Override
    public void excludeUserFromGroups(IUser user, Collection<Long> groupIds) {
        log.info("exclude user {} to groups by ids:{}", user, groupIds);
        try {
            em.getTransaction().begin();
            User u = (User) load(user);
            if (u == null) {
                log.warn("can't found user:{}", user);
                return;
            }
            if (groupIds == null || groupIds.isEmpty()) {
                log.warn("groups  can't be empty");
                return;
            }
            Set<Long> newGroups = new HashSet<Long>();
            for (Group g : u.getGroups()) {
                newGroups.add(g.getId());
            }
            newGroups.removeAll(groupIds);

            Set<Group> uGroup = new HashSet<Group>();

            if (!newGroups.isEmpty()) {
                List<Group> groups = PersistenceUtils.loadEntitiesByIds(em, Group.class, newGroups);
                uGroup.addAll(groups);
            }
            u.setGroups(uGroup);
            em.merge(u);
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("can't exclude user {} from groups {}", user, groupIds);
            log.debug("", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }

    }*/

/*    @Override
    public Collection<IGroup> findAllowedGroups(Long permission) {
        if (permission == null) {
            log.warn("permission can't be null");
            return EMPTY_GROUPS_SET;
        }
        try {
            TypedQuery<Group> tq = em.createNamedQuery("group.findAllowGroups", Group.class)
                    .setParameter("groupPermission", permission);
            List<Group> gs = tq.getResultList();

            Set<IGroup> groups = new HashSet<IGroup>(gs);
            if (log.isDebugEnabled()) {
                for (IGroup iGroup : groups) {
                    log.debug("group in result: {}", iGroup);
                }
            }
            return groups;
        } catch (NoResultException e) {
            log.warn("could not found allowed groups by permission:{}", permission);
            log.debug("trace: ", e);
            return EMPTY_GROUPS_SET;
        }

    }*/

/*    @Override
    public Collection<IRole> findAllowedRoles(Long permission) {
        if (permission == null) {
            log.warn("permission can't be null");
            return EMPTY_ROLES_SET;
        }
        try {
            TypedQuery<Role> tq = em.createNamedQuery("role.findAllowRoles", Role.class)
                    .setParameter("rolePermission", permission);
            List<Role> rs = tq.getResultList();
            Set<IRole> roles = new HashSet<IRole>(rs);
            if (log.isDebugEnabled()) {
                for (IRole role : roles) {
                    log.debug("role in result: {}", role);
                }
            }
            return roles;
        } catch (NoResultException e) {
            log.warn("could not found allowed groups by permission:{}", permission);
            log.debug("trace: ", e);
            return EMPTY_ROLES_SET;
        }
    }*/

/*    @Override
    public boolean isAllowedUserAccess(IUser user, IGroup group) {
        if (group == null || group.getPermission() == null) {
            log.warn("group can't be null");
            return false;
        }
        IUser u = load(user);
        if (u == null) {
            log.warn("can't found user: {}", user);
            return false;
        }

        for (IGroup gr : findUserGroupsByUserId((Long) u.getId())) {
            if (gr.getPermission() <= group.getPermission()) {
                log.debug("user :{} has allowed access to group:{}", user, group);
                return true;
            }
        }
        log.debug("user :{} hasn't allowed access to group:{}", user, group);
        return false;
    }*/

/*    @Override
    public boolean isAllowedUserAccess(IUser user, IRole role) {
        if (role == null || role.getPermission() == null) {
            log.warn("role can't be null");
            return false;
        }
        IUser u = load(user);
        if (u == null) {
            log.warn("can't found user: {}", user);
            return false;
        }
        for (IRole r : findUserRolesByUserId((Long) u.getId())) {
            if (r.getPermission() <= role.getPermission()) {
                log.debug("user :{} has allowed access to role:{}", user, role);
                return true;
            }
        }
        log.debug("user :{} hasn't allowed access to role:{}", user, role);
        return false;
    }*/


//    @Override
    public IUser newInstance() {
        return new User();
    }

    /*@Override
    public IRole newRoleInstance() {
        return new Role();
    }*/

    /*@Override
    public IGroup newGroupInstance() {
        return new Group();
    }*/


/*    @Override
    @SuppressWarnings("unchecked")
    public void reassignUserRoles(IUser user, Collection<IRole> roles) {
        Set<Long> roleIds = new HashSet<Long>();
        for (IRole<Long> r : roles)
            roleIds.add(r.getId());
        reassignUserRoleById(user, roleIds);

    }*/

/*
    @Override
    @SuppressWarnings("unchecked")
    public void reassignUserGroups(IUser user, Collection<IGroup> groups) {
        Set<Long> groupIds = new HashSet<Long>();
        for (IGroup<Long> g : groups)
            groupIds.add(g.getId());
        reassignUserGroupById(user, groupIds);
    }
*/

/*    @Override
    public void reassignUserRoleById(IUser user, Collection<Long> roleIds) {
        log.info("reassign user {} role by id {}", user, roleIds);
        try {
            em.getTransaction().begin();

            User u = (User) load(user);
            if (u == null) {
                log.warn("can't find user {}", user);
                return;
            }
            if (roleIds == null || roleIds.isEmpty()) {
                log.debug("remove all roles for user:{}", user);
                u.getRoles().clear();
                em.merge(u);
                return;
            }

            Set<Role> uRoles = new HashSet<Role>(PersistenceUtils.loadEntitiesByIds(em, Role.class, roleIds));
            u.setRoles(uRoles);
            em.merge(u);
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("can't reassign user {} by roles id {}", user, roleIds);
            log.debug("", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }
    }*/

/*    @Override
    public void reassignUserGroupById(IUser user, Collection<Long> groupIds) {
        log.info("reassign user {} group by id {}", user, groupIds);
        try {
            em.getTransaction().begin();
            User u = (User) load(user);
            if (u == null) {
                log.warn("can't find user {}", user);
                return;
            }
            if (groupIds == null || groupIds.isEmpty()) {
                log.debug("remove all groups for user:{}", user);
                u.getGroups().clear();
                em.merge(u);
                return;
            }

            Set<Group> uGroups = new HashSet<Group>(PersistenceUtils.loadEntitiesByIds(em, Group.class, groupIds));
            u.setGroups(uGroups);
            em.merge(u);
            em.getTransaction().commit();
        } catch (Exception e) {
            log.error("can't reassign user {} by groups id{}", user, groupIds);
            log.debug("", e);
        }finally {
            if(em.getTransaction().isActive()){
                em.getTransaction().rollback();
            }
        }

    }*/

/*
    @Override
    public void enableUserToRoles(IUser user, Collection<IRole> roles) {
        Set<Long> roleIds = new HashSet<Long>();
        for (IRole r : roles) {
            roleIds.add((Long) r.getId());
        }
        includeUserToRoles(user, roleIds);
    }

    @Override
    public void enableUserToGroups(IUser user, Collection<IGroup> groups) {
        Set<Long> groupIds = new HashSet<Long>();
        for (IGroup g : groups)
            groupIds.add((Long) g.getId());
        includeUserToGroups(user, groupIds);
    }

    @Override
    public void disableUserFromRoles(IUser user, Collection<IRole> roles) {
        Set<Long> roleIds = new HashSet<Long>();
        for (IRole r : roles) {
            roleIds.add((Long) r.getId());
        }
        excludeUserFromRoles(user, roleIds);
    }

    @Override
    public void disableUserFromGroups(IUser user, Collection<IGroup> groups) {
        Set<Long> groupIds = new HashSet<Long>();
        for (IGroup g : groups)
            groupIds.add((Long) g.getId());
        excludeUserFromGroups(user, groupIds);
    }

    @Override
    public Long findUserMaxPermissionByRole(IUser user) {
        Number result = Long.MAX_VALUE;
        try {
            result = em.createNamedQuery("user.findRoleWithMaxPermission", Number.class).setParameter("userId", user.getId()).getSingleResult();
            log.info("max permission: {} for user: {}", result.longValue(), user);
        } catch (NoResultException e) {
            log.error("hasUserAccessToRolesId: not result found for user:", user);
            log.debug("trace: ", e);

        } catch (NonUniqueResultException e) {
            log.error("findUserMaxPermissionByRole: not unique result for user:", user);
            log.debug("trace: ", e);
        }
        return result.longValue();
    }

    @Override
    public boolean hasUserAccessToRolesId(IUser user, Collection<Long> roles) {
        List<Role> rs = PersistenceUtils.loadEntitiesByIds(em, Role.class, roles);
        Long maxPermission = findUserMaxPermissionByRole(user);
        for (Role role : rs) {
            if (role.getPermission() >= maxPermission) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean hasUserAccessToRolesByName(IUser user, Collection<String> rolesName) {
        List<Role> rs = PersistenceUtils.loadEntitiesByName(em, Role.class, rolesName);
        Long maxPermission = findUserMaxPermissionByRole(user);
        for (Role role : rs) {
            if (role.getPermission() >= maxPermission) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean hasUserAccessToRoles(IUser user, Collection<IRole> roles) {
        Set<Long> ids = new HashSet<Long>();
        for (IRole role : roles) {
            ids.add((Long) role.getId());
        }
        return hasUserAccessToRolesId(user, ids);
    }

    @Override
    public boolean hasUserAccessToGroups(IUser user, Collection<String> groupsName) {
        for (IGroup group : findUserGroupsByUserId((Long) user.getId())) {
            if (groupsName.contains(group.getName())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean hasUserAccessToGroups(IUser user, Collection<IGroup> groups) {
        for (IGroup group : findUserGroupsByUserId((Long) user.getId())) {
            if (groups.contains(group)) {
                return true;
            }
        }
        return false;
    }
*/


//    @Override
    public void setUserName(IUser user, String newUserName) {
        User u = (User) load(user);
        if (u == null) {
            log.warn("can't find user:{}", user);
            return;
        }
        u.setName(newUserName);
        update(u);
    }

//    @Override
    public void setPassword(IUser user, String password) {
        User u = (User) load(user);
        if (u == null) {
            log.warn("can't find user:{}", user);
            return;
        }
        u.setPassword(password);
        update(u);
    }

//    @Override
    public void setHint(IUser user, String hint) {
        User u = (User) load(user);
        if (u == null) {
            log.warn("can't find user:{}", user);
            return;
        }
        u.setHint(hint);
        update(u);
    }

//    @Override
    public void setNickName(IUser user, String nickName) {
        User u = (User) load(user);
        if (u == null) {
            log.warn("can't find user:{}", user);
            return;
        }
        u.setNickName(nickName);
        update(u);
    }

/*    @Override
    public IRole newRoleInstance(String roleName, Long permission) {
        Role role = (Role) newRoleInstance();
        role.setName(roleName);
        role.setPermission(permission);
        return role;
    }

    @Override
    public IGroup newGroupInstance(String groupName, Long permission) {
        Group group = (Group) newGroupInstance();
        group.setName(groupName);
        group.setPermission(permission);
        return group;
    }*/


}
